#ifndef VC1DEC_DEFINITIONS_H_
#define VC1DEC_DEFINITIONS_H_
typedef unsigned char   U8_VC1;
typedef char            I8_VC1;
typedef short           I16_VC1;
typedef unsigned short  U16_VC1;
typedef int             I32_VC1;
typedef unsigned int    U32_VC1;

#define VOID_VC1 void
#define NULL     0

#define NUM_BLOCKS 6

#define ABS(x) ((x)>0?(x):(-(x)))
#define MAX(x,y) (((x)>=(y))?(x):(y))
#define MIN(x,y) (((x)<=(y))?(x):(y))

#define VC1_FALSE 0
#define VC1_TRUE  1
#define VC1_NULL  0

#define INTRA  0
#define INTER  1

#define VC1CBPVLCPEEKBITS 8
#define VC1DCDIFFVLCPEEKBITS 12
#define VC1ACPEEKBITS     8

#define VC1DCDIFFESCAPE 119
#define ESCAPE          ((unsigned short)(-2))

typedef enum {
	VC1_eProfileSimple = 0,
	VC1_eProfileMain,
	VC1_eProfileAdvanced,
	VC1_eProfileReserved
}VC1_eProfile;

typedef enum
{
    VC1_LevelLow    = 0,    /** Simple/Main profile low level */
    VC1_LevelMedium = 1,    /** Simple/Main profile medium level */
    VC1_LevelHigh   = 2,    /** Simple/Main profile high level */

    VC1_LevelL0     = 0,    /** Advanced profile level 0 */
    VC1_LevelL1     = 1,    /** Advanced profile level 1 */
    VC1_LevelL2     = 2,    /** Advanced profile level 2 */
    VC1_LevelL3     = 3,    /** Advanced profile level 3 */
    VC1_LevelL4     = 4,    /** Advanced profile level 4 */

    /* 5 to 7 reserved */

    VC1_LevelUnknown = 255  /** Unknown profile */
} VC1_eLevel;

typedef enum {
	VC1_PICTURE_I = 0,
	VC1_PICTURE_P,
	VC1_PICTURE_B,
	VC1_PICTURE_BI,
	VC1_PICTURE_SKIP
}VC1_ePictureType;

typedef enum
{
    VC1_ProgressiveFrame = 0,   
    VC1_InterlacedFrame  = 1,
    VC1_InterlacedField  = 2,
    VC1_PictureFormatNone
} vc1_ePictureFormat;

typedef struct{
	U8_VC1* pStreamStart;
	U8_VC1* pStreamEnd;
	U32_VC1 Cache;
    I32_VC1 BitValid;
	I32_VC1 BitUsed;
	I32_VC1 ByteUsed;
}sVC1Stream;

typedef struct{
	U8_VC1* Y;
	U8_VC1* U;
	U8_VC1* V;
	I32_VC1  stride;
	I32_VC1  display;///0---can 
}sVC1Picture;

typedef struct
{
    U8_VC1  Numerator;      /** BFraction numerator */
    U8_VC1  Denominator;    /** BFraction denominator */
    U8_VC1  ScaleFactor;    /** Approximated Numerator*256/Denominator */
} VC1_sBFraction;

typedef enum
{
    VC1_MVRange64_32    = 0,    /* x=-64 to 63.f by y=-32 to 31.f */
    VC1_MVRange128_64   = 1,
    VC1_MVRange512_128  = 2,
    VC1_MVRange1024_256 = 3
} VC1_eMVRange;

typedef struct
{
	U16_VC1 run;
	U16_VC1 level;
	U16_VC1 last;
}sVC1RL;
typedef enum
{
    VC1_MVMode1MVHalfPelBilinear = 0,   /** 1MV     0.50 pel bilinear  */
    VC1_MVMode1MVHalfPel         = 1,   /** 1MV     0.50 pel bicubic   */
    VC1_MVMode1MV                = 2,   /** 1MV     0.25 pel bicubic   */
    VC1_MVModeMixedMV            = 3,   /** MixedMV 0.25 pel bicubic   */
    VC1_MVModeIntensityCompensation     /** Variable length code escape flag */
} VC1_eMVMode;

typedef enum {
	MB_Direct = 1,
	MB_Forward,
	MB_Backward,
	MB_Interpolate,
	MB_Intra,
}eVC1MBType;

typedef struct {
	eVC1MBType mbtype;
	U8_VC1 mvmode;
	U8_VC1 skip;
	U8_VC1 acpred;
	U8_VC1 MBCoded;///0---intra  1----inter

	U8_VC1 CBP;
	U8_VC1 subCBP[NUM_BLOCKS];
	U8_VC1 isIntra[NUM_BLOCKS];

	/////quantized DC AC
	I16_VC1 default_dc;
	I16_VC1 DC[NUM_BLOCKS];
	I16_VC1 acTop[NUM_BLOCKS * 7];
	I16_VC1 acLeft[NUM_BLOCKS * 7];

	U32_VC1 mquant;
	U32_VC1 boundary;
}sVC1MB;

typedef struct{
	I16_VC1 code;
	I16_VC1 len;
}sVC1VLCCode;


typedef struct _VC1DEC{
	sVC1Stream stream;

	/////sequence header
	VC1_eProfile profile;
	VC1_eLevel   level;
	U8_VC1       frmrtq;
	U8_VC1       bitrtq;
	U8_VC1       loopfilter;
	U8_VC1       multires;
	U8_VC1       fastuvmc;
	U8_VC1       ext_mv;
	U8_VC1       dquant;
	U8_VC1       vstransform;
	U8_VC1       overlap;
	U8_VC1       syncmarker;
	U8_VC1       rangered;
	U8_VC1       maxbframes;
	U8_VC1       quantizer;
	U8_VC1       finterpflag;

	U8_VC1       colordif_format;
	U8_VC1       postprocflag;
	U8_VC1       pulldown;
	U8_VC1       interlace;
	U8_VC1       psf;
	U8_VC1       aspect_w;
	U8_VC1       aspect_h;
	U8_VC1       frameratenr;
	U8_VC1       frameratedr;
	U8_VC1       reserve0;
	U16_VC1      framerateexp;//28 char

	U8_VC1       color_frim;
	U8_VC1       transfer_char;
	U8_VC1       matrix_coef;

	U8_VC1       hrd_param_flag;
	U8_VC1       hrd_num_leaky_buckets;
	U8_VC1       bit_rate_exponent;
	U8_VC1       buffer_size_exponent;
	U8_VC1       reserve1;//36 char

	U16_VC1      hrd_rate_array[16];
	U16_VC1      hrd_buffer_array[16];

	///entry point
	U8_VC1       broken_link;
	U8_VC1       closed_entry;
	U8_VC1       panscanflag;
	U8_VC1       refdistflag;
	U8_VC1       hrd_full[16];
	U8_VC1       extended_dmv;
	U8_VC1       range_mapy;
	U8_VC1       range_mapuv;///aligned

	///picture header
	U8_VC1       rangedfrm;
	VC1_ePictureType ePictureType;
	U8_VC1       bfraction;
	U8_VC1       bf;

	U8_VC1       pquant;
	U8_VC1       nonuniform;

	U8_VC1       halfqp;
	U8_VC1       pquantizer;
	VC1_eMVRange       mvrange;
	U8_VC1       respic;

	VC1_eMVMode  mvmode;
	VC1_eMVMode  mvmode2;
	U8_VC1       lumscale;
	U8_VC1       lumshift;

	U8_VC1       bitplanerawmode;//1-----raw mode

	U8_VC1       mvtab;
	U8_VC1       cbptab;

	U8_VC1       dquantfrm;
	U8_VC1       dqbilevel;
	U8_VC1       boundryedge;/////bottom<<3|right<<2|top<<1|left
	U8_VC1       altpquant;

	U8_VC1       ttmbf;
	U8_VC1       ttfrm;

	U8_VC1       transacfrm;
	U8_VC1       transacfrm2;
	U8_VC1       transdctab;

	////slice header

	//////MB

	////block
	I32_VC1      first_mode3;
	I32_VC1      level_size;
	I32_VC1      run_size;



	///s/m profile
	I32_VC1      width;
	I32_VC1      height;
	/////entry point
	I32_VC1      coded_width;
	I32_VC1      coded_height;
	////advanced profile sequence
	I32_VC1      maxwidth;
	I32_VC1      maxheight;
	I32_VC1      dis_width;
	I32_VC1      dis_height;

	I32_VC1      hrd_buffer;
	I32_VC1      hrd_rate;
	I32_VC1      framerate;

	I32_VC1      widthMBs;
	I32_VC1      heightMBs;
	I32_VC1      numMBs;

	I32_VC1      firstframe;

	sVC1Picture *pRef0Picture;
	sVC1Picture *pRef1Picture;
	sVC1Picture *pBPicture;
	sVC1Picture *pCurPicture;
	sVC1Picture *pOutPicture;
	U8_VC1      *bitplane;
	sVC1MB      *pAllMBs;

	/////VLC tables
	sVC1VLCCode *pICBPYTableSM;
	sVC1VLCCode *pPBCBPYTableSM[4];
	sVC1VLCCode *pLumaDCTables[2];
	sVC1VLCCode *pChroDCTables[2];
	sVC1VLCCode *pYACIRLTable[6];//0~2-----pqindex<=8  3~5-------pqindex > 8
	sVC1VLCCode *pUVACIRLTable[6];//

	sVC1RL      *pACRLTables[6];
	U16_VC1     *pACDeltaRunTables_0[6];
	U16_VC1     *pACDeltaRunTables_1[6];
	U16_VC1     *pACDeltaLevelTables_0[6];
	U16_VC1     *pACDeltaLevelTables_1[6];

	sVC1VLCCode *pLumaDCTable;
	sVC1VLCCode *pChroDCTable;
	sVC1VLCCode *pACIRLTable;
	sVC1RL      *pACRLTable;
	U16_VC1     *pACDeltaRunTable_0;
	U16_VC1     *pACDeltaRunTable_1;
	U16_VC1     *pACDeltaLevelTable_0;
	U16_VC1     *pACDeltaLevelTable_1;

}sVC1Decoder;
#endif